# -*- coding: utf-8 -*-
# @Time: 2021/11/27 23:12
# @Author: lijinxi
# @File    : user_number_delay.py
# @desc

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns

# sns.set(color_codes=True)
from matplotlib.ticker import MultipleLocator

mpl.rcParams["axes.unicode_minus"] = False

# 柱高信息
Y = [5.895, 6.012, 6.234, 6.4985]
Y1 = [4.023, 4.234, 4.514, 4.6183]
Y2 = [1.002, 1.0578, 1.15022, 1.1777]
Y3 = []
for i in range(4):
    Y[i] = Y[i] * 1000 / 200
    Y1[i] = Y1[i] * 1000 / 200
    Y2[i] = Y2[i] * 1000 / 200
    Y3.append((Y1[i] + Y2[i] + Y[i]) / 50)
X = np.arange(len(Y))

bar_width = 0.2
tick_label = ['50', '100', '150', '200']

axes = plt.gca()
axes.set_ylim([0, 55])
# 绘制柱状图
plt.tick_params(labelsize=20)  # 刻度字体大小13
plt.bar(X, Y, bar_width - 0.04, align="center", color='white', edgecolor="olivedrab", label="CD", hatch='+')
plt.bar(X + bar_width, Y1, bar_width - 0.04, color='white', edgecolor="teal", align="center",
        label="QD", hatch='/')
plt.bar(X + bar_width * 2, Y2, bar_width - 0.04, color='white', edgecolor="royalblue", align="center",
        label="SD", hatch='-')

ax = plt.gca()
ax.tick_params(labelsize=16)

plt.ylabel("Average Delay (ms)", fontsize=18)
plt.xlabel("User Number", fontsize=18)
# plt.title()

plt.xticks(X + bar_width * 1.0, tick_label)
# 显示图例
plt.legend(fontsize=18, ncol=1, frameon=False)

y_major_locator = MultipleLocator(10)
ax = plt.gca()

ax.yaxis.set_major_locator(y_major_locator)
plt.axhline(y=10, color='lightgray', ls=':', linewidth=1)
plt.axhline(y=20, color='lightgray', ls=':', linewidth=1)
plt.axhline(y=30, color='lightgray', ls=':', linewidth=1)
plt.axhline(y=40, color='lightgray', ls=':', linewidth=1)
plt.axhline(y=50, color='lightgray', ls=':', linewidth=1)

plt.savefig('average_delay.pdf', bbox_inches='tight')  # 保存成PDF放大后不失真（默认保存在了当前文件夹下）
plt.show()
